package cn.matezk.cloud.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.matezk.cloud.dao.SecretKeyDao;
import cn.matezk.cloud.dto.SecretKeyDto;
import cn.matezk.cloud.model.DeviceBind;
import cn.matezk.cloud.model.SecretKey;
import cn.matezk.cloud.service.ISecretKeyService;
import cn.matezk.cloud.vo.SecretKeyVo;
import com.github.pagehelper.PageInfo;
import jdk.nashorn.internal.runtime.regexp.joni.exception.InternalException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SecretKeyService implements ISecretKeyService {
    @Autowired
    private SecretKeyDao secretKeyDao;

    @Override
    public List<SecretKey> selectAll() {
        return secretKeyDao.selectAll();
    }

    @Override
    public PageInfo<SecretKeyVo> query(SecretKeyDto secretKeyDto) {
        List<SecretKeyVo> list = secretKeyDao.query(secretKeyDto);
        return new PageInfo<>(list);
    }

    @Override
    public SecretKey save(SecretKey secretKey) {
        SecretKey db = secretKeyDao.selectByAppKey(secretKey.getAppKey());
        if (ObjectUtil.isNotNull(db))   throw new InternalException("appKey已存在");
        secretKeyDao.insertSelective(secretKey);
        return secretKey;
    }

    @Override
    public int update(SecretKey secretKey) {
        return secretKeyDao.updateByPrimaryKeySelective(secretKey);
    }

    @Override
    public int bind(DeviceBind deviceBind) {
        secretKeyDao.unbind(deviceBind);
        return secretKeyDao.bind(deviceBind);
    }
}
